﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.Net;

namespace PSE.LogPackager.Common.Configuration
{
    public class LogPackagerConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("supportEmail", IsRequired = false)]
        public string SupportEmail
        {
            get { return (string)this["supportEmail"]; }
            set { this["supportEmail"] = value; }
        }

        [ConfigurationProperty("sources", IsRequired = true)]
        [ConfigurationCollection(typeof(LogSourceConfigurationCollection), AddItemName = "source")]
        public LogSourceConfigurationCollection Sources
        {
            get { return (LogSourceConfigurationCollection)this["sources"]; }
        }

        public Uri GetAddress(string logSource)
        {
            LogSourceConfigurationElement source = this.Sources.Get(logSource);

            if (source == null)
                throw new Exception(string.Format("Cannot get log packager service address, '{0}' source not found.", logSource));

            // Captura o endereço do serviço
            UriBuilder address = new UriBuilder(source.ServiceAddress);
            // Troca o host do endereço pelo dns do servidor
            address.Host = Dns.GetHostEntry(Dns.GetHostName()).HostName;

            return address.Uri;
        }

        public Binding GetBinding(Uri address)
        {
            Binding binding = null;

            switch (address.Scheme.ToLower())
            {
                case "net.tcp":
                    binding = new NetTcpBinding();
                    ((NetTcpBinding)binding).Security.Mode = SecurityMode.None;
                    ((NetTcpBinding)binding).TransferMode = TransferMode.Streamed;
                    ((NetTcpBinding)binding).MaxReceivedMessageSize = Int32.MaxValue;
                    ((NetTcpBinding)binding).MaxBufferSize = Int32.MaxValue;
                    ((NetTcpBinding)binding).ReaderQuotas.MaxStringContentLength = Int32.MaxValue;

                    ((NetTcpBinding)binding).PortSharingEnabled = true;
                    break;

                case "net.pipe":
                    binding = new NetNamedPipeBinding();
                    ((NetNamedPipeBinding)binding).Security.Mode = NetNamedPipeSecurityMode.None;
                    ((NetNamedPipeBinding)binding).TransferMode = TransferMode.Streamed;
                    ((NetNamedPipeBinding)binding).MaxReceivedMessageSize = Int32.MaxValue;
                    ((NetNamedPipeBinding)binding).MaxBufferSize = Int32.MaxValue;
                    ((NetNamedPipeBinding)binding).ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
                    break;

                case "http":
                    binding = new BasicHttpBinding();
                    ((BasicHttpBinding)binding).Security.Mode = BasicHttpSecurityMode.None;
                    ((BasicHttpBinding)binding).TransferMode = TransferMode.Streamed;
                    ((BasicHttpBinding)binding).MaxReceivedMessageSize = Int32.MaxValue;
                    ((BasicHttpBinding)binding).MaxBufferSize = Int32.MaxValue;
                    ((BasicHttpBinding)binding).ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
                    break;

                default:
                    throw new Exception(string.Format("Cannot create binding from '{0}' log packager service address.", address.ToString()));
            }

            binding.SendTimeout = new TimeSpan(0, 10, 0);
            binding.OpenTimeout = new TimeSpan(0, 10, 0);
            binding.CloseTimeout = new TimeSpan(0, 10, 0);
            binding.ReceiveTimeout = new TimeSpan(0, 10, 0);

            return binding;
        }
    }
}
